home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
tex
/
sspell14.zip
/
ROOT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-07-18
|
13KB
|
460 lines
/* **************************************************************** */
/* sspell - similar to Unix spell */
/* version 1.4 */
/* */
/* Author: Maurice Castro */
/* Release Date: 4 Jul 1992 */
/* Bug Reports: maurice@bruce.cs.monash.edu.au */
/* */
/* This code has been placed by the Author into the Public Domain. */
/* The code is NOT covered by any warranty, the user of the code is */
/* solely responsible for determining the fitness of the program */
/* for their purpose. No liability is accepted by the author for */
/* the direct or indirect losses incurred through the use of this */
/* program. */
/* */
/* Segments of this code may be used for any purpose that the user */
/* deems appropriate. It would be polite to acknowledge the source */
/* of the code. If you modify the code and redistribute it please */
/* include a message indicating your changes and how users may */
/* contact you for support. */
/* */
/* The author reserves the right to issue the official version of */
/* this program. If you have useful suggestions or changes for the */
/* code, please forward them to the author so that they might be */
/* incorporated into the official version */
/* */
/* Please forward bug reports to the author via Internet. */
/* */
/* **************************************************************** */
/* determine the root of a word using the rules in the rule file */
#include <stdio.h>
#if !defined(pyr)
#include <stdlib.h>
#endif
#include "strfn.h"
#include "root.h"
#include "error.h"
#include "utility.h"
#include "string.h"
#define MAXSTR 128
#define PRE 0
#define POST 1
typedef struct rule
{
char *presuf; /* prefix / suffix */
char *required; /* character string required at end of word */
char *forbid; /* character string forbidden at end of word */
char *del; /* characters to be deleted from end of word */
struct rule *next;
} RULE;
RULE *prefixlist;
RULE *suffixlist;
void badline(lno)
int lno;
{
fprintf(stderr, "Warning: line %d in the rule file is badly formed\n",
lno);
}
void memfail()
{
errormesg("Error: Out of Memory! Terminating",-3);
}
/* read the rules file */
/* this file is made up of lines of the form:
/* pre|post <prefix/suffix> <required> <forbiden> <delete> */
/* Any blank fields should contain a single "-" all fields separated by */
/* white space. A "#" in the first character position indicates a comment */
void initroot(file)
char *file;
{
FILE *f;
char instr[MAXSTR];
char *token;
int lno;
char *presuf; /* prefix / suffix */
char *required; /* character string required at end of word */
char *forbid; /* character string forbidden at end of word */
char *del; /* characters to be deleted from end of word */
int prepost;
RULE *newitem;
RULE *prefixlast;
RULE *suffixlast;
prefixlist = NULL;
suffixlist = NULL;
f = fopen(file,"rt");
lno = -1;
while (!feof(f))
{
if (fgets(instr,MAXSTR-1,f)==NULL)
break;
lno++;
if (instr[0] == '#')
continue; /* handle comments */
token = strtok(instr," \t\n");
if (!strcmp(token, "pre"))
prepost = PRE;
else if (!strcmp(token, "post"))
prepost = POST;
else
{
badline(lno);
continue;
}
/* deal with the rest of line */
token = strtok(NULL," \t\n");
if (token == NULL)
{
badline(lno);
continue;
}
if (!strcmp(token,"-"))
presuf = NULL;
else
{
presuf = (char *) malloc(strlen(token)+1);
if (presuf == NULL) memfail();
lstrcpy(presuf,token);
}
token = strtok(NULL," \t\n");
if (token == NULL)
{
badline(lno);
continue;
}
if (!strcmp(token,"-"))
required = NULL;
else
{
required = (char *) malloc(strlen(token)+1);
if (required == NULL) memfail();
lstrcpy(required,token);
}
token = strtok(NULL," \t\n");
if (token == NULL)
{
badline(lno);
continue;
}
if (!strcmp(token,"-"))
forbid = NULL;
else
{
forbid = (char *) malloc(strlen(token)+1);
if (forbid == NULL) memfail();
lstrcpy(forbid,token);
}
token = strtok(NULL," \t\n");
if (token == NULL)
{
badline(lno);
continue;
}
if (!strcmp(token,"-"))
del = NULL;
else
{
del = (char *) malloc(strlen(token)+1);
if (del == NULL) memfail();
lstrcpy(del,token);
}
newitem = (RULE *) malloc(sizeof(RULE));
if (newitem == NULL) memfail();
(*newitem).presuf = presuf;
(*newitem).required = required;
(*newitem).forbid = forbid;
(*newitem).del = del;
(*newitem).next = NULL;
if (prepost == PRE)
{
if (prefixlist == NULL)
{
prefixlist = newitem;
prefixlast = newitem;
}
else
{
(*prefixlast).next = newitem;
prefixlast = newitem;
}
}
else
{
if (suffixlist == NULL)
{
suffixlist = newitem;
suffixlast = newitem;
}
else
{
(*suffixlast).next = newitem;
suffixlast = newitem;
}
}
}
fclose(f);
}
int compend(a, b)
char *a;
char *b;
{
char *sstr;
sstr = a+strlen(a)-strlen(b);
if (!stricmp(sstr,b))
return(1);
return(0);
}
/* determine the root of a given word */
int prefix(in, outlst)
char *in;
WORDLST **outlst;
{
RULE *pt;
char *sstr;
int idx;
int outflg;
WORDLST *newitem;
*outlst = NULL;
outflg = 0;
pt = prefixlist;
while (pt != NULL)
{
if (!strnicmp(pt->presuf, in, strlen(pt->presuf)))
{
outflg = 1;
in += (strlen(pt->presuf));
/* add it to the output list */
newitem = (WORDLST *) malloc (sizeof(WORDLST));
newitem->prefix = NULL;
newitem->suffix = NULL;
if (newitem == NULL)
{
memfail();
}
newitem->word = (char *) malloc(strlen(in)+1);
if (newitem->word == NULL)
{
memfail();
}
lstrcpy(newitem->word, in);
newitem->prefix = (char *) malloc(strlen(pt->presuf)+2);
if (newitem->prefix == NULL)
{
memfail();
}
lstrcpy(newitem->prefix, pt->presuf);
strcat(newitem->prefix, "+");
newitem->next = *outlst;
*outlst = newitem;
}
pt = pt->next;
}
if (outflg) return(1);
return(0);
}
int suffix(in, outlst)
char *